﻿Chenxin.UI.EmployeePermitsMgr=Chenxin.extend(Chenxin.UI.PageCommon,{
	actionUrl:'service/EmployeePermitsList.ashx',
	createGrid:function(){		
		return new Chenxin.UI.Pagination({
			actionUrl:this.actionUrl+'?op=getlist',
			id:'epl',
			renderTo:'epl_table'
		});
	},
	initEvent:function(){
		var _this=this;
		$('#btn_setting').click(function(){
			_this.add();
			//_this.form.reset();
			//$('#tr_rolelist').html('请选择角色公司！');
		});
		$('.del_role').live('click',function(){
			_this.currentId=$(this).attr('name');
			_this.del();
		});
	},
	createSearch:function(){
		return new Chenxin.UI.EmployeePermitsSearch({
			scope:this
		});
	},
	createForm:function(){
		return new Chenxin.UI.EmployeePermitsForm({
			scope:this,
			saveSucc:function(){
				this.grid.reload('','',true);
			}
		});
	},
	initComponent:function(){
		Chenxin.UI.EmployeePermitsMgr.superclass.initComponent.call(this);
	}
});

Chenxin.UI.EmployeePermitsForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/EmployeePermitsList.ashx',
    renderTo: 'jqm_Epf',
    id: 'epfWin',
    jqmWidth: 500,
    title: '用户角色权限设定',
    customerFormVal: function(model) {
        if (!model.RoleIdList) {
            alert('请勾选角色！');
            return false;
        }
        return true;
    },
    afterGetFormJson: function(model) {
		delete model.UserName;
		delete model.UserCode;
        Chenxin.apply(this.form.additionalParam, model);
		this.form.additionalParam.UserId=this.form.currentUserId;
    },
    createComboList: function() {
        var _this = this;
        

        //城市
        var city = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#' + this.id + '_role_city',
            inModelType: 'none',
            tbxWidth: 120,
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    company.clearItems();
                } else {
                    company.param.fatherid = this.getValue();
                    company.reload();
                }
            }
        });

        //公司
        var company = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getcompany' },
            containerId: '#' + this.id + '_role_company',
            inModelType: 'none',
            tbxWidth: 120,
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    _this.loadRole();
                } else {
                    _this.loadRole(this.getValue());
                }
            }
        });

        return [city, company];
    },
    loadRole: function(companyid) {
        if (companyid) {
            Chenxin.ajax({
                url: this.actionUrl + '?op=getrole',
                type: 'get',
                dataType: 'json',
                data: { companyid: companyid },
                onSuccess: function(data, msg) {
                var html = '<ul style="width:400px;">';
                    for (var i = 0; i < data.items.length; i++) {
                        html += '<li style="width:90;float:left;"><input type="checkbox" name="RoleIdList" value="' + data.items[i].val + '" />' + data.items[i].txt + '</li>';
                    }
                    html += '</ul>';
                    $('#tr_rolelist').html(html);
                }
            });
        } else {
            $('#tr_rolelist').text('请选择角色公司!');
        }
    },
    createForm: function() {
        return Chenxin.FormCommon.employeePermitsForm(this.id);
    },
	initFormValidator:function(){
		
		var _this=this;
		var filter='#'+this.id;
		$(filter+'old_usercode').formValidator({
			validatorgroup:this.id,
            onshow: "请输入用户名!", 
			onfocus: "请输入用户名!", 
			oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, 
			empty: { 
				leftempty: false, 
				rightempty: false, 
				emptyerror: "用户名不能为空!" 
			}, 
			onerror: "用户名不能为空!"
        }).functionValidator({
            fun: function(val, elem) {
                var exist = true;
				Chenxin.ajax({
					type: "get",
					dataType: "json",
					async: false,
					url: 'service/OrganizationMgr.ashx',
					data: { op: 'getuserbycode', usercode: val},
					onSuccess: function(data) {
						if(data.userinfo){
							_this.findByName('UserName').val(data.userinfo);
							_this.currentUserId=data.UserId;
						}
						exist=data.exist;
					}
				});
				if (!exist) {
					return '用户名不存在';
				} else {
					return true;
				}
            },
            onerror: '用户名不存在！'
        });
	},
    initEvent: function() {
        var _this = this;
        $('#epfWin_Save').live('click', function() {
            _this.save();
        });
        $('#epfWin_Cancle').live('click', function() {
            _this.hide();
        });
		
    },
    initComponent: function() {
        Chenxin.UI.EmployeePermitsForm.superclass.initComponent.call(this);
    }
});

Chenxin.UI.EmployeePermitsSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'epl_search',
    createComboList: function() {
        var struct = new Chenxin.UI.StructComboList({
            companyAfterSelected: function(val) {
                if (val == this.company.defVal) {
                    role.clearItems();
                } else {
                    role.param.companyid = val;
                    role.reload();
                }
            },
            companyAfterLoadItem: function(items) {
                if (items.length == 1) {
                    role.param.companyid = items[0].val;
                    role.reload();
                }
            },
            tbxWidth: 60
        });
        var role = new Chenxin.UI.Combo({
            containerId: '#role',
            actionUrl: 'service/EmployeePermitsList.ashx',
            name: 'roleid',
            autoLoad: false,
            colWidth: 120,
            tbxWidth: 60,
            
            param: { op: 'getrole' }
        });
        //虚帐号
        var cmbIsVirtual = new Chenxin.UI.Combo({
            containerId: '#Virtual',
            name: 'virtual',
            tbxWidth: 50,
            items: comboCommon.IsVirtual
        });
        struct.comboList.push(role);
        struct.comboList.push(cmbIsVirtual);
        return struct.comboList;
    },
    initEvent: function() {
        var _this = this;
        $('#btn_search').live('click', function() {
            _this.scope.reloadGrid();
        });
    },
    initComponent: function() {
        Chenxin.UI.EmployeePermitsSearch.superclass.initComponent.call(this);
    }
});


Chenxin.FormCommon.employeePermitsForm=function(comboIdPrefix){
	return '<table class="tbl_f"><tbody><tr><th style="width:80px;">工号：</th><td><input id="'+comboIdPrefix+'old_usercode" name="UserCode" class="inputbox_n fl" type="text"  style="width:160px;" /><div id="'+comboIdPrefix+'old_usercodeTip" style="float: left; margin-left: 5px; width: 90; height: 26px; line-height: 26px; text-align: left;" class="fl"></div></td></tr><tr id="'+comboIdPrefix+'_tr_username"><th></th><td><input name="UserName" class="inputbox_n fl" style="width:160px;" readonly="readonly"  /></td></tr><tr><th>角色城市：</th><td><div style="z-index:906; margin-right: 5px;" id="'+comboIdPrefix+'_role_city" class="SelectWrapSingle"></div></td></tr><tr><th>角色公司：</th><td><div style="z-index:905; margin-right: 5px;" id="'+comboIdPrefix+'_role_company" class="SelectWrapSingle"></div></td></tr><tr><th>用户角色：</th><td id="tr_rolelist">请选择角色公司!</td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></tbody></table>';
};

$(document).ready(function(){
	var eplMgr=new Chenxin.UI.EmployeePermitsMgr();
});



